/*
 * Copyright (c) 2014 laidian. All Rights Reserved.
 * 
 * @author yaoyuming
 * 
 * @date 2020-04-07 10:39
 */
package com.yao.common.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 并发线程池
 * 
 * @author yaoyuming
 * @date 2020/04/07 10:39
 */
@Configuration
@ConditionalOnProperty(prefix = "async.configuration", name = "enable", havingValue = "true")
@Slf4j
public class ThreadPoolConfig {

    @Bean("y-task")
    public Executor executor() {
        log.debug("--->开启线程池配置<---");

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(200);

        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("y-thread-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        return executor;
    }

}